<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>向东软件后台管理</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="../assets/public/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../assets/public/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../assets/public/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../assets/public/css/AdminLTE.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="../assets/public/css/blue.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<body class="hold-transition register-page">
<div class="register-box">
  <div class="register-logo">
    <a href=""><b>向东软件后台</b>注册</a>
  </div>

  <div class="register-box-body">
    <p class="login-box-msg">Register a new membership</p>

       <input type="hidden" name="_csrf" value="<?= Yii::$app->security->generateRandomString()?>"> 
      <div class="form-group has-feedback">
        <input type="text" class="form-control" name="username" placeholder="用户名">
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <span class="user" style="color:red;"></span>
      <div class="form-group has-feedback">
        <input id="txtEmail" type="email" class="form-control" name="email" placeholder="邮箱">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <span class="email" style="color:red;"></span>
      <div class="form-group has-feedback">
        <input type="password" class="form-control" name="pwd" placeholder="密码">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <span class="pwd" style="color:red;"></span>
      <div class="form-group has-feedback">
        <input type="password" class="form-control" name="repwd" placeholder="确认密码">
        <span class="glyphicon glyphicon-log-in form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">
          
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <button type="submit" class="btn btn-primary btn-block btn-flat">注册</button>
        </div>
        <!-- /.col -->
      </div>

    <a href="login" class="text-center">我已有账号</a>
  </div>
  <!-- /.form-box -->
</div>
<!-- /.register-box -->

<!-- jQuery 3 -->
<script src="../assets/public/js/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="../assets/public/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="../assets/public/js/icheck.min.js"></script>
<script>
    $(function () {
        $('input').iCheck({
          checkboxClass: 'icheckbox_square-blue',
          radioClass: 'iradio_square-blue',
          increaseArea: '20%' /* optional */
        });
        //用户名验证
        var us = false;
        $('[name="username"]').focus(function(event) {     
                if (state == false) {
                    $(this).val('');
                }
        });
        $('[name="username"]').focusout(function(event) {
            if ($('[name="username"]').val().length<2) {
                $(".user").text('用户名长度不能小于2个字符');
                $(this).focus();
            }else{
                $(".user").text('');
                us = true;
            }
        });
        //邮箱验证
            var state = false;
            $('#txtEmail').focus(function () {
                if (state == false) {
                    $(this).val('');
                }
            })
            $('#txtEmail').blur(function () {
                if ($(this).val() == '') {
                    $('.email').text('邮箱不能为空');
                    $(this).focus();
                }
                else {
                    if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val()) == false) {
                        $('.email').text('邮箱格式不正确，请重新输入');
                        $(this).focus();
                    }
                    else {
                        $('.email').text('');
                        state = true;
                    }
                }
            });
        //密码验证
            var pa = false;
            $('[name="pwd"]').focus(function(event) {  
                if(pa == false){   
                    $(this).text('');
                }
            });
            $('[name="pwd"]').blur(function(event) {
                if ($(this).val().length<6) {
                    $(".pwd").text('用户名长度不能小于6个字符');
                    $(this).focus();
                }else{
                    $(".pwd").text('');
                    pa = true;
                }
            });

        $(".btn").click(function(){

            var username = $('[name="username"]').val();
            var email = $('[name="email"]').val();      
            var password_hash = $('[name="pwd"]').val();  
            var repwd = $('[name="repwd"]').val();
            var auth_key = $('[name="_csrf"]').val();
            if (username == '' || email == '' || repwd == ''){
                alert('请填完整信息');
            }else{

                    if(password_hash != repwd){
                        alert('两次密码不一致');
                    }else{
                    
                        $.ajax({
                            type:'post',
                            url:'doregister',
                            async:true,
                            cache:false,/*url写异域的请求地址*/
                            data:{
                                username:username,email:email,password_hash:password_hash,auth_key:auth_key
                            },
                            success: function(datas){
                                //console.log(datas);
                                if(datas == 1){
                                    alert('注册成功，请登录');
                                    window.location.href = 'login';
                                }else if (datas == 2) {
                                    alert('用户已存在');
                                }else if (datas == 3) {
                                    alert('邮箱已存在');
                                }else{
                                    alert('注册失败')
                                }
                            }
                        });
                    }
                }
        });

    });

</script>
</body>
</html>





